﻿package com.fendou.virtualphone.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {

	// 数据库名
	private final String DB_NAME = "callphoneapp.db";
	// 数据库版
	private final int DB_VERSION = 1;

	private Context mContext = null;
	public SQLiteDatabase mSQLiteDatabase = null;
	public DatabaseHelper mDatabaseHelper = null;
	public final String MYUID = "my_uid";

	/**
	 * 来电历史记录
	 */
	public final String TABLE_CALLHISTORY = "callHistoryTable";
	public final String CALLHISTORY_ID = "id";// id
	public final String CALLHISTORY_NAME = "name";//
	public final String CALLHISTORY_PHONE_NUM = "phone_num";//
	public final String CALLHISTORY_CALL_TIME = "call_time";// 留格式： yyyyMMddHHmm
	public final String CALLHISTORY_RING_MODE = "ring_mode";// 1-震动 2-铃声 3.震动及铃声
	public final String CALLHISTORY_CURRENT_CALL = "current_call";//    0 :当前来电
	// 0：表示设置的电话，1：接到的电话     2.不出现在历史记录中
	public final String CALLHISTORY_CALL_TYPE = "call_type";
	
	
	/**
	 * 创建买家留言表
	 */
	private String SQL_CREATE_BUYERMSG_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_CALLHISTORY + " (" + CALLHISTORY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + CALLHISTORY_NAME
			+ " VARCHAR(20)," + CALLHISTORY_PHONE_NUM + " VARCHAR(20),"
			+ CALLHISTORY_CALL_TIME + " VARCHAR(20)," 
			+ CALLHISTORY_CURRENT_CALL + " VARCHAR(10)," 
			+ CALLHISTORY_CALL_TYPE + " VARCHAR(10)," 
			+ CALLHISTORY_RING_MODE
			+ " VARCHAR(10))";

	public DBAdapter(Context context) {
		mContext = context;
	}

	private class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);

		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(SQL_CREATE_BUYERMSG_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			// alert table 原表A to 临时表temp_A
			// create 表A
			// insert into A select from temp_A
		}
	}

	/**
	 * 数据库升�?
	 * 
	 * @author wubo
	 * @createtime 2012-9-12
	 */
	public void updateTable() {

	}

	// 打开数据�?
	public synchronized void open() {
		try {
			mDatabaseHelper = new DatabaseHelper(mContext);
			if (mSQLiteDatabase == null) {
				mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	// 关闭数据�?
	public void close() {
		if (mDatabaseHelper != null) {
			mDatabaseHelper.close();
		}
	}

	public Cursor rawQuery(String sql) {
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(sql, null);
		} catch (Exception e) {
		}
		return mCursor;
	}

	public Cursor rawQuery(String sql, String[] value) {
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(sql, value);
		} catch (Exception e) {
		}
		return mCursor;
	}

	// 插入�?��数据
	public long insertData(String table, ContentValues initialValues) {
		return mSQLiteDatabase.insert(table, "_id", initialValues);
	}

	// 通过Cursor查询�?��指定字段的数�?
	public Cursor getAllDatas(String table, String[] keyString) {
		return mSQLiteDatabase.query(table, keyString, null, null, null, null,
				null);
	}

	// 通过Cursor查询�?��指定字段的数�?
	public Cursor getDatasbyLimit(String table, String[] keyString,
			String limitString) {
		return mSQLiteDatabase.query(table, keyString, null, null, null, null,
				null, limitString);
	}

	// 按条件，通过Cursor查询指定字段的数�?
	public Cursor getAllDatas(String table, String[] keyString, String key,
			String value) {
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.query(true, table, keyString, key + "='"
					+ value + "'", null, null, null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
		} catch (Exception e) {
		}
		return mCursor;
	}

	// 通过Cursor查询�?��数据
	public Cursor getAllDatas(String table) {
		String selectSQL = "select * from " + table;
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(selectSQL, null);
		} catch (Exception e) {
		}
		return mCursor;
	}

	// 通过Cursor查询�?��数据
	public Cursor getAllDatas1(String table, String order) {
		String selectSQL = "select * from " + table + " Order By " + order;
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(selectSQL, null);
		} catch (Exception e) {
		}
		return mCursor;
	}

	// 按条件，通过Cursor查询数据
	public Cursor getAllDatas(String table, String wherekey, String wherevalue) {
		String selectSQL = "select * from " + table + " where " + wherekey
				+ " = '" + wherevalue + "'";
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(selectSQL, null);
		} catch (Exception e) {
		}
		return mCursor;
	}


	/**
	 * /** �?�?
	 * 
	 * @author wubo
	 * @createtime 2012-9-8
	 * @param sql
	 * @param values
	 */
	public void deleteOrUpdate(String sql, Object[] values) {
		mSQLiteDatabase.execSQL(sql, values);
	}

	/**
	 * 查询
	 * 
	 * @author wubo
	 * @createtime 2012-9-8
	 * @param sql
	 * @param values
	 * @return
	 */
	public Cursor select(String sql, String[] values) {
		Cursor mCursor = null;
		try {
			mCursor = mSQLiteDatabase.rawQuery(sql, values);
		} catch (Exception e) {
			return null;
		}
		return mCursor;
	}

}
